//
//  EntertainViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "EntertainViewController.h"
#import "EnterttainTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "EnterttainModel.h"
#import "AFNetBlock.h"
#import <AVFoundation/AVFoundation.h>

#import "XqEntViewController.h"
#import "XqEnterTableViewCell.h"
#import "XqEntModel.h"
#import "ImageTableViewCell.h"
#import "ImageModel.h"
#import "XqimageViewController.h"

#import "MoveiTableViewCell.h"
#import "MoveiModel.h"
#import "MJRefresh.h" // 刷新

@interface EntertainViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property(nonatomic, retain)UITableView *tableView;//段子
@property(nonatomic, retain)UITableView *pageTableView;//图片
@property(nonatomic, retain)UITableView *tableViewVC;// 视频
@property(nonatomic, retain)NSMutableArray *dateArr;//段子数组
@property(nonatomic, retain)NSMutableArray *pageDateArr;//图片数组
@property(nonatomic, retain)NSMutableArray *arrayVC;
@property(nonatomic, retain)UIScrollView *scrollView;//轮播
@property(nonatomic, retain)UISegmentedControl *segment; // 分段控制器
@property(nonatomic, retain)NSMutableString *pagestr;
@property(nonatomic, retain)AVPlayer *player;//播放器
@property(nonatomic, retain)AVPlayerItem *playerItem;//播放对象
@property(nonatomic, retain)UIView *playView;//播放区域
@property(nonatomic, retain)AVPlayerLayer *playerLayer;//显示图层
@property(nonatomic, retain)UIView *header;
@property(nonatomic, retain)UIView *footer;
@property(nonatomic, retain)NSString *strrr;
@end

@implementation EntertainViewController
- (void)dealloc
{
    [_header release];
    [_footer release];
    [_strrr release];
    [_playerLayer release];
    [_playView release];
    [_playerItem release];
    [_player release];
    [_pagestr release];
    [_segment release];
    [_scrollView release];
    [_tableView release];
    [_pageTableView release];
    [_tableViewVC release];
    [_dateArr release];
    [_pageDateArr release];
    [_arrayVC release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    
    /** scrollView视图设置 **/
    [self scrollViewsz];
    /** 设置分段控制器 **/
    [self segments];

    
#pragma mark - 段子tableView设置
    // 段子tableView创建
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 60) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    [_scrollView addSubview:_tableView];
    // 段子重用池
    [_tableView registerClass:[EnterttainTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    // 数据获取
    NSString *str = @"http://s.budejie.com/topic/list/jingxuan/29/budejie-android-6.2.8/0-20.json?market=xiaomi&udid=862966024235029&appname=baisibudejie&os=4.2.2&client=android&visiting=&mac=8c%3Abe%3Abe%3A5c%3A9f%3A66&ver=6.2.8";
    [AFNetBlock GetDataWithURL:str block:^(id block) {
        NSArray *array = block[@"list"];
        self.dateArr = [EnterttainModel modelHandlerWithArray:array];
       
        
        // 刷新页面
        
        [_tableView reloadData];
        
    }];
#pragma mark - 图片tableView设置
// 图片tableView创建
    self.pageTableView  = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64) style:UITableViewStylePlain];
    
    self.pageTableView.delegate = self;
    self.pageTableView.dataSource = self;
//    _pageTableView.backgroundColor = [UIColor yellowColor];
    [_scrollView addSubview:_pageTableView];
    // 图片重用池
    [_pageTableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"pageCell"];
    
     [_pageTableView release];
    /** 图片数据获取 **/
    NSString *pageStr = @"http://s.budejie.com/topic/list/zuixin/10/budejie-android-6.3.0/0-20.json?market=tencentyingyongbao&udid=862966024235029&appname=baisibudejie&os=4.2.2&client=android&visiting=&mac=8c%3Abe%3Abe%3A5c%3A9f%3A66&ver=6.3.0";
    [AFNetBlock GetDataWithURL:pageStr block:^(id block) {
        NSArray *array = block[@"list"];
       self.strrr = block[@"info"][@"np"];
        self.pageDateArr = [ImageModel modelHandlerWithArray:array];
        /**下拉刷新 **/
        self.pageTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        [self.pageTableView.header beginRefreshing];
        /** 上拉加载**/
        self.pageTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        
        // 刷新页面
        
        [_pageTableView reloadData];
        

    }];

#pragma mark - 视频创建
    self.tableViewVC = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width *2 , 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
//    _tableViewVC.backgroundColor = [UIColor blueColor];
    self.tableViewVC.delegate = self;
    self.tableViewVC.dataSource = self;
    [_scrollView addSubview:_tableViewVC];
    
    // 视频重用池
    [_tableViewVC registerClass:[MoveiTableViewCell class] forCellReuseIdentifier:@"cellVC"];
    [_tableViewVC release];
    
    /** 视频数据获取 **/
    NSString *strVC = @"http://s.budejie.com/topic/list/jingxuan/41/budejie-android-6.3.1/1456316822-20.json?market=xiaomi&udid=862966024235029&appname=baisibudejie&os=4.2.2&client=android&visiting=&mac=8c%3Abe%3Abe%3A5c%3A9f%3A66&ver=6.3.1";
    [AFNetBlock GetDataWithURL:strVC block:^(id block) {
        NSArray *arrVC = block[@"list"];

        self.arrayVC = [MoveiModel modelHandlerWithArray:arrVC];
        // 刷新页面
        
        [_tableViewVC reloadData];
    
    
    }];
    
}
- (void)buttAction{
    
}


#pragma mark -scrollView设置
- (void)scrollViewsz{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 )];
    _scrollView.backgroundColor = [UIColor yellowColor];
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    [self.view addSubview:_scrollView];
    [_scrollView release];
}

#pragma mark - 分段控制器
- (void)segments{
    _segment = [[UISegmentedControl alloc]initWithItems:@[@"段子", @"图片", @"视频"]];
    _segment.frame = CGRectMake(0, 0, 150, 40);
    [self.view addSubview:_segment];
    
    // 边框颜色
    _segment.tintColor = [UIColor blackColor];
    // 设置默认分段索引
    _segment.selectedSegmentIndex = 0;
    //添加到导航栏中间
    self.navigationItem.titleView = _segment;
    [_segment release];
    
    // 添加点击事件
    [_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    
}
#pragma mark 分段控制器点击事件
- (void)segmentAction:(UISegmentedControl *)segme{
    if (segme.selectedSegmentIndex == 0) {
        _scrollView.contentOffset =CGPointMake(self.view.frame.size.width *0, 0);
    }
    if (segme.selectedSegmentIndex ==1) {
        _scrollView.contentOffset = CGPointMake(self.view.frame.size.width *1, 0);
    }
    if (segme.selectedSegmentIndex == 2) {
        _scrollView.contentOffset = CGPointMake(self.view.frame.size.width *2, 0);
        
    }
}

// 滚动视图控制分段控制器
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        _segment.selectedSegmentIndex = scrollView.contentOffset.x / self.view.frame.size.width;
    }
}

#pragma mark - tableVirw协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _tableView) {
        return _dateArr.count;
    }else if(tableView == _pageTableView){
        return _pageDateArr.count;
    }else {
      return _arrayVC.count;  
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     // 段子
    if (tableView == _tableView ) {
        EnterttainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        EnterttainModel *m = _dateArr[indexPath.row];
        cell.texLable.text = m.text;
        cell.nameLable.text = m.name;
        cell.timeLable.text = m.passtime;
        cell.zanLable.text = m.up;
        cell.caiLable.text = [m.down stringValue];
        cell.reviewLable.text = m.comment;
        cell.relayLable.text  = m.forward;
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:m.header] placeholderImage:[UIImage imageNamed:@"zwt.jpg"]];//头像图片
        cell.zanImage.image = [UIImage imageNamed:@"zan"];
        cell.caiImage.image = [UIImage imageNamed:@"cai"];
        cell.reviewImage.image = [UIImage imageNamed:@"zf"];
        cell.relayImage.image = [UIImage imageNamed:@"pl"];
        cell.hotLable.text =@"吐槽";
        
        return cell;
        
        }else if(tableView ==_pageTableView){
            // 图片
        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pageCell" forIndexPath:indexPath];
        ImageModel *m = _pageDateArr[indexPath.row];
        cell.nameLable.text = m.name;
        cell.timeLable.text = m.passtime;
        cell.texLable.text = m.text;
        cell.heightE = m.height; // 静态高
        cell.widthE = m.width; // 静态宽
        cell.widthD = m.widthD; // 动态宽
        cell.heightD = m.heightD;// 动态高
        cell.judge = m.big;
            cell.zanLable.text = m.up;
            cell.caiLable.text = [m.down stringValue];
            cell.relayLable.text  = m.forward;
            cell.reviewLable.text = m.comment;
            cell.zanImage.image = [UIImage imageNamed:@"zan"];
            cell.caiImage.image = [UIImage imageNamed:@"cai"];
            cell.reviewImage.image = [UIImage imageNamed:@"zf"];
            cell.relayImage.image = [UIImage imageNamed:@"pl"];
            cell.hotLable.text =@"吐槽";
          
//      cell.imagId = m.imageId;//ID
            
//             NSLog(@"%@%@%@%@", m.width, m.widthD, m.heightD, m.height);
//
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:m.header]placeholderImage:[UIImage imageNamed:@"zwt.jpg"]];
        // 判断是否是动图
        if (m.images != nil) {
            [cell.imag sd_setImageWithURL:[NSURL URLWithString:m.images]placeholderImage:[UIImage imageNamed:@"zwt.jpg"]];

        }else{
            [cell.imag sd_setImageWithURL:[NSURL URLWithString:m.big]placeholderImage:[UIImage imageNamed:@"zwt.jpg"]];

        }
        
         return cell;
        }else {
            // 视频
            MoveiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellVC" forIndexPath:indexPath];
          MoveiModel *m = _arrayVC[indexPath.row];
            cell.nameLable.text = m.name;
            cell.timeLable.text = m.passtime;
            cell.texteLable.text = m.text;
            [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:m.header]];
            [cell.imag sd_setImageWithURL:[NSURL URLWithString:m.thumbnail]];
            NSLog(@"%@%@", m.width, m.height);
           return cell;
        }
    
}
#pragma mark - 高度设置
//cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == _tableView) {
        EnterttainModel *m = _dateArr[indexPath.row];
        CGFloat h = [EnterttainTableViewCell heightWithText:m.text];
        
        return h + 60 + 40 + 50;
    }else if(tableView == _pageTableView){
        
        ImageModel *m = _pageDateArr[indexPath.row];
        CGFloat texth =[ImageTableViewCell heightWithText:m.text];
        //通过判断是否是动图添加高度
        CGFloat h;
        if (m.big != nil) {
            h =[ImageTableViewCell heightWithHeight:m.height width:m.width];
        }else{
            
            h =[ImageTableViewCell heightWithHeight:m.heightD width:m.widthD];
        }

        return texth + 60 + 40 + h + 50 ;
    }
    else{
       MoveiModel *m = _arrayVC[indexPath.row];
        CGFloat h =[MoveiTableViewCell heightWithText:m.text];
        
//       CGFloat vh =[MoveiTableViewCell heightWithHeight:m.height width:m.width];
        
        return 60 + h ;
    }
    
}
// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == _tableView) {
        // 取消选中状态
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        XqEntViewController *xq = [[XqEntViewController alloc]init];
        // 传值给下一页
        
        xq.model = _dateArr[indexPath.row];
        
        [self.navigationController pushViewController:xq animated:YES];
    }else if(tableView == _pageTableView){
    
        XqimageViewController *xqimage = [[XqimageViewController alloc]init];
            xqimage.mo = _pageDateArr[indexPath.row];
        [self.navigationController pushViewController:xqimage animated:YES];
    
    }
    
}
 /** 上拉加载方法调用 **/
- (void)loadMoreData{
    NSString *pageStr = @"http://s.budejie.com/topic/list/zuixin/10/budejie-android-6.3.0/";
    NSString *strOne =@"-20.json?market=tencentyingyongbao&udid=862966024235029&appname=baisibudejie&os=4.2.2&client=android&visiting=&mac=8c%3Abe%3Abe%3A5c%3A9f%3A66&ver=6.3.0";
    NSString *strTow = [NSString stringWithFormat:@"%@%@%@", pageStr, _strrr, strOne];
    [AFNetBlock GetDataWithURL:strTow block:^(id block) {
        NSArray *array = block[@"list"];
        self.strrr = block[@"info"][@"np"];
        // 转化成model类型
        NSArray *modelArr = [ImageModel modelHandlerWithArray:array];
        // 添加在数组中
        for (ImageModel *model in modelArr) {
            
            [_pageDateArr addObject:model];
        }
        
        // 刷新页面
        
        [_pageTableView reloadData];
        
        self.pageTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        
    }];
    
    

}
    /** 下拉刷新方法调用 **/
- (void)loadNewData{
    NSString *pageStr = @"http://s.budejie.com/topic/list/zuixin/10/budejie-android-6.3.0/0-20.json?market=tencentyingyongbao&udid=862966024235029&appname=baisibudejie&os=4.2.2&client=android&visiting=&mac=8c%3Abe%3Abe%3A5c%3A9f%3A66&ver=6.3.0";
    [AFNetBlock GetDataWithURL:pageStr block:^(id block) {
        NSArray *array = block[@"list"];
        self.strrr = block[@"info"][@"np"];
        self.pageDateArr = [ImageModel modelHandlerWithArray:array];
        
        [_pageTableView reloadData];
        
   }];
    //停止刷新
    [_pageTableView.header endRefreshing];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
